package com.example.zhihuigongdi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhihuigongdi.commer.CommonException;
import com.example.zhihuigongdi.entity.Personnel;
import com.example.zhihuigongdi.entity.Recordings;
import com.example.zhihuigongdi.entity.User;
import com.example.zhihuigongdi.entity.respon.Result;
import com.example.zhihuigongdi.mapper.RecordingsMapper;
import com.example.zhihuigongdi.service.IRecordingsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhihuigongdi.utils.CheckUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuhongbin
 * @since 2024-04-17
 */
@Service
public class RecordingsServiceImpl extends ServiceImpl<RecordingsMapper, Recordings> implements IRecordingsService {

    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    @Lazy
    private IRecordingsService recordingsService;
    @Override
    public Result addRecording(Recordings recordings) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        recordingsService.save(recordings);
        return Result.success("添加成功");
    }

    @Override
    public Result removeRecording(int recording_id) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }

        QueryWrapper<Recordings> recordingsQueryWrapper = new QueryWrapper<>();
        recordingsQueryWrapper.eq("recording_id",recording_id);
        recordingsService.remove(recordingsQueryWrapper);
        return Result.success("删除成功");
    }

    @Override
    public Result getRecording(int recording_id) {
        return null;
    }

    @Override
    public Result getRecordings(int current, int size) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        PageHelper.startPage(current,size);
        List<Recordings> list = recordingsService.list();
        PageInfo<Recordings> personnelPageInfo = new PageInfo<>(list);
        return Result.success(personnelPageInfo);

    }
}
